<?php
/**注意此文档为UTF-8的编码.
 * Created on 2010-5-29
 *
 * @author EXT <isrn@qq.com>
 *
 */
include ('../../includes/global.php');
include ('../global.php');
include ("../power.php");
$dbcfg=Sys::Gp(array('dbcfg','sevenel'));

switch($_POST['op']){
		case 'grid_data':{
 		$temp=Dgd(array(
		  'db'=>'d_sevenel',
		  'cd'=>'1',
		  'tn'=>"datamod"
		  ));
		foreach ($temp as $key=>$value){
    	$temp[$key]['id_mirror']=$temp[$key]['id'];
    	}
		echo json_encode($temp);
		break;}
	case 'shownowtable':{
 		$temp=Dgd(array(
		  'db'=>'d_sevenel',
		  'cd'=>'2',
		  //'ce'=>1,
		  'tn'=>"datamod"
		  ));
		foreach ($temp as $key=>$value){
    	$temp[$key]['id_mirror']=$temp[$key]['id'];
    	//$temp[$key]['shield']=;
    	}
		echo json_encode($temp);
		break;
		}
 	case 'save':{
		$save['name']=$_POST['name'];
		$save['declare']=$_POST['declare'];
		$tableExist=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'sql'=>"show tables"
		));
		foreach($tableExist as $arraw=>$value){
		  		$table_exist[]=$value['Tables_in_'.$dbcfg['database']];
		}
		$tablename=$dbcfg['prefix'].'_'.$_POST['tabname'];
		if(in_array($tablename,$table_exist)){
			die(ext_request('此表在数据库已存在[The tabel is already exists in database]!'));		
					}
		preg_match("/^[\x4e00-\x9fa5]+$/",$tablename) or die(ext_request('表名必须为英文[The table name must be in English]'));
		$save['created']='1';
		if($_POST['id_mirror']==0){//add
			$save['tabname']=$_POST['tabname'];
			$save['extend_id']=$_POST['extend_id'];
			$save['extend_name']=$_POST['extend_name'];
			if($save['extend_id']!=0){
				$extend_id=$save['extend_id'];
				$extendNameTemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $extend_id",
		  		'tn'=>"datamod",
		  		'limit'=>'1'
				));
				$extendName=$dbcfg['prefix'].'_'.$extendNameTemp['tabname'];
				if(!in_array($tablename,$table_exist)){
					$d_sevenel->Desql("create table ".$tablename." like ".$extendName) or die ('数据表创建失败[Table to create failure]!');
				};
			}else{
				$returnid=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"2",
		  		'tn'=>"datamod",
		  		'column'=>'max(id)'
		 		 ));
				$d_sevenel->Desql("create table ".$dbcfg['prefix']."_".$save['tabname']." like ".$dbcfg['prefix']."_father");
			}
			Di('sevenel','datamod',$save);
			ext_request('ok');
		}else{
			if(Du('sevenel','datamod',$save,'id',$_POST['id_mirror'])){
				ext_request('ok');
			}else{
				ext_request('修改表失败[Modified table failure]');
			}
		}
 	break;}
	case 'del':{
 	   $id=$_POST['id'];
 	   $tablenametemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		 		 ));
 			if(Dd('sevenel','datamod','id',$id,0,1)){
		 		$tablename=$dbcfg['prefix'].'_'.$tablenametemp[0]['tabname'];
 				$d_sevenel->Desql("Alter TABLE ".$tablename." RENAME TO ".$tablename."_bak_".randomkeys(8));
 				echo 'ok';
 			}else{
 				echo '删除表失败[Delete table failure]';
 			}
 	break;}
	case 'check':{
 	   $id=$_POST['id'];
 	   $tablenametemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		 		 ));
	   $tablename=$dbcfg['prefix'].'_'.$tablenametemp[0]['tabname'];
		  $temp= $d_sevenel->Dqsql("CHECK TABLE `".$tablename."` ");
 			if($temp[0]['Msg_text']=='OK'){
 				echo 'ok';
 			}else{
 				echo "$temp[0]['Msg_text']";
 			}
 	break;}
	case 'yh':{
 	   $id=$_POST['id'];
 	   $tablenametemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		 		 ));
	   $tablename=$dbcfg['prefix'].'_'.$tablenametemp[0]['tabname'];
		  $temp= $d_sevenel->Dqsql("OPTIMIZE TABLE `".$tablename."` ");
 			if($temp[0]['Msg_text']=='OK'){
 				echo 'ok';
 			}else{
 				echo $temp[0]['Msg_text'];
 			}
 	break;}
	case 'xf':{
 	   $id=$_POST['id'];
 	   $tablenametemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		 		 ));
	   $tablename=$dbcfg['prefix'].'_'.$tablenametemp[0]['tabname'];
		  $temp= $d_sevenel->Dqsql("REPAIR TABLE `".$tablename."` ");
 			if($temp[0]['Msg_text']=='OK'){
 				echo 'ok';
 			}else{
 				echo $temp[0]['Msg_text'];
 			}
 	break;}
	case 'fx':{
 	   $id=$_POST['id'];
 	   $tablenametemp=Dgd(array(
		  		'db'=>'d_sevenel',
		  		'cd'=>"id = $id",
		  		'tn'=>"datamod"
		 		 ));
	   $tablename=$dbcfg['prefix'].'_'.$tablenametemp[0]['tabname'];
		  $temp= $d_sevenel->Dqsql("ANALYZE TABLE `".$tablename."` ");
 			if($temp[0]['Msg_text']=='OK'){
 				echo 'ok';
 			}else{
 				echo $temp[0]['Msg_text'];
 			}
 	break;}

	}

?>
